<html><head><title>UserGuide - Some Tips & Tricks</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="5">UserGuide - Some Tips & Tricks</font></b></p>

<p><b>"Using a map to index a list"</b></p><blockquote>


<a href="newlist.html">lists</a> provide a powerful way to build a structured 
storage system - however they have a disadvantage. If you aren't sure exactly where 
in the list a particular item is, you must examine each entry in the list to find the 
right one. 
<br>
<br>
<a href="newmap.html">Maps</a> also provide a similar function but are indexed by a 
key value instead - however they too have a disadvantage, they don't maintain the 
order elements are inserted into the list. 
<br>
<br>
However by using a combination of the two, you can neatly avoid both of these issues... 
<br>
<br>
This example loads a <a href="structures.html">structured</a> <a href="newlist.html">list</a> 
of book data and builds an index of ISBN numbers using a <a href="newmap.html">map</a>. 
It then demonstrates how to access the list using the index in the map. 

<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">EnableExplicit</font></b>
  
  <font color="#3A3966">; A book catalog structure.</font>
  <b><font color="#3A3966">Structure</font></b> BOOK
    Title.s
    Author.s
    ISBN13.s
    Price.d
  <b><font color="#3A3966">EndStructure</font></b>
  
  <font color="#3A3966">; Create a list to hold the catalog entries.</font>
  <b><font color="#3A3966">NewList</font></b> <font color="#3A3966">Catalog</font>.BOOK()
  <font color="#3A3966">; Create a map to hold the ISBN index.</font>
  <b><font color="#3A3966">NewMap</font></b> <font color="#3A3966">ISBN13</font>.l()
  <font color="#3A3966">; Working variables.</font>
  <b><font color="#3A3966">Define</font></b>.l Count, Index
  <b><font color="#3A3966">Define</font></b>.s ISBN
  
  <font color="#3A3966">; Add an empty element at the top of the list.</font>
  <font color="#3A3966">; The first element in a list is numbered zero, however the map will return zero if a requested entry isn't present.</font>
  <font color="#3A3966">; This empty element avoids a potential problem with an incorrect reference to the first catalog item being returned.</font>
<font color="#3A3966">  AddElement</font>(<font color="#3A3966">Catalog</font>())
  
  <b><font color="#3A3966">For</font></b> Count = 1 <b><font color="#3A3966">To</font></b> 5
    
    <font color="#3A3966">; Read the data from the table into the list.</font>
<font color="#3A3966">    AddElement</font>(<font color="#3A3966">Catalog</font>())
    <b><font color="#3A3966">Read</font></b>.s <font color="#3A3966">Catalog</font>()\Title
    <b><font color="#3A3966">Read</font></b>.s <font color="#3A3966">Catalog</font>()\Author
    <b><font color="#3A3966">Read</font></b>.s <font color="#3A3966">Catalog</font>()\ISBN13
    <b><font color="#3A3966">Read</font></b>.d <font color="#3A3966">Catalog</font>()\Price
    
    <font color="#3A3966">; Index the ISBN to the map.</font>
<font color="#3A3966">    ISBN13</font>(<font color="#3A3966">Catalog</font>()\ISBN13) =<font color="#3A3966"> ListIndex</font>(<font color="#3A3966">Catalog</font>())
    
  <b><font color="#3A3966">Next</font></b> Count
  
  <font color="#3A3966">; Find an entry.</font>
  ISBN = &quot;978-0340896983&quot;
  Index =<font color="#3A3966"> ISBN13</font>(ISBN)
  
  <b><font color="#3A3966">If</font></b> Index &gt; 0
    <b><font color="#3A3966">Debug</font></b> &quot;Book with ISBN13 &quot; + ISBN + &quot; is at list index &quot; +<font color="#3A3966"> StrU</font>(Index) + &quot;.&quot;
    <b><font color="#3A3966">Debug</font></b> &quot;&quot;
    
    <font color="#3A3966">; We can now directly select the right list element without having to perform a search.</font>
<font color="#3A3966">    SelectElement</font>(<font color="#3A3966">Catalog</font>(), Index)
    <b><font color="#3A3966">Debug</font></b> &quot;'&quot; +<font color="#3A3966"> Catalog</font>()\Title + &quot;' by &quot; +<font color="#3A3966"> Catalog</font>()\Author
    <b><font color="#3A3966">Debug</font></b> &quot;ISBN: &quot; +<font color="#3A3966"> Catalog</font>()\ISBN13
    <b><font color="#3A3966">Debug</font></b> &quot;Price: &quot; +<font color="#3A3966"> StrD</font>(<font color="#3A3966">Catalog</font>()\Price, 2)
    
  <b><font color="#3A3966">Else</font></b>
    <b><font color="#3A3966">Debug</font></b> &quot;No book with that ISBN in the catalog.&quot;
    
  <b><font color="#3A3966">EndIf</font></b>
  
  <b><font color="#3A3966">End</font></b>
  
  <font color="#3A3966">; Some test data.</font>
  <b><font color="#3A3966">DataSection</font></b>
    
    BookData:
    
    <b><font color="#3A3966">Data</font></b>.s &quot;Carte Blanche&quot;, &quot;Jeffery Deaver&quot;, &quot;978-1444716474&quot;
    <b><font color="#3A3966">Data</font></b>.d 19.99
    
    <b><font color="#3A3966">Data</font></b>.s &quot;One Day&quot;, &quot;David Nicholls&quot;, &quot;978-0340896983&quot;
    <b><font color="#3A3966">Data</font></b>.d 7.99
    
    <b><font color="#3A3966">Data</font></b>.s &quot;Madeleine&quot;, &quot;Kate McCann&quot;, &quot;978-0593067918&quot;
    <b><font color="#3A3966">Data</font></b>.d 20.00
    
    <b><font color="#3A3966">Data</font></b>.s &quot;The Dukan Diet&quot;, &quot;Dr Pierre Dukan&quot;, &quot;978-1444710335&quot;
    <b><font color="#3A3966">Data</font></b>.d 8.99
    
    <b><font color="#3A3966">Data</font></b>.s &quot;A Game of Thrones&quot;, &quot;George R. R. Martin&quot;, &quot;978-0006479888&quot;
    <b><font color="#3A3966">Data</font></b>.d 9.99
    
    <b><font color="#3A3966">Data</font></b>.s &quot;The Help&quot;, &quot;Kathryn Stockett&quot;, &quot;978-0141039282&quot;
    <b><font color="#3A3966">Data</font></b>.d 8.99
    
  <b><font color="#3A3966">EndDataSection</font></b>
</font></pre>


</blockquote>
<p><b>UserGuide Navigation</b></p><blockquote>

<a href="ug_advanced.html">< Previous: Advanced functions</a> | <a href="ug_overview.html">Overview</a> 
</body></html>